package cn.maihe.elg.operation.model.enums;

import java.util.Arrays;

/**
 * @Description 内部系统模块流转响应码
 * @Author WJH
 * @Date 2021/03/25
 */
public enum InnerRespCode {

    SUCCESS("0000", "明确成功"),
    FAILURE("0100", "明确失败"),
    FAILURE_PARAM("0101", "参数校验失败"),
    EXCEPTION("9900", "异常错误"),
    TIMEOUT("0200", "响应超时"),
    ;

    //明确成功
    private static String SUCCESSP_REFIX = "00";
    //明确失败
    private static String FAILURE_REFIX = "01";
    //超时未知
    private static String TIMEOUT_REFIX = "02";
    //异常错误
    private static String EXCEPTION_REFIX = "99";

    private String respCode;
    private String respDesc;

    InnerRespCode(String respCode, String respDesc) {
        this.respCode = respCode;
        this.respDesc = respDesc;
    }

    public String getRespCode() {
        return respCode;
    }

    public String getRespDesc() {
        return respDesc;
    }

    public InnerRespCode resolve(String respCode) {
        return Arrays.stream(values()).filter(v -> v.getRespCode().equals(respCode)).findFirst().orElseThrow(() -> new RuntimeException("未知响应码[" + respCode + "错误]"));
    }

    public boolean isSuccess() {
        return getRespCode().startsWith(SUCCESSP_REFIX);
    }

    public boolean isFailure() {
        return getRespCode().startsWith(FAILURE_REFIX);
    }

    public boolean isException() {
        return getRespCode().startsWith(EXCEPTION_REFIX);
    }

    public boolean isTimeOut() {
        return getRespCode().startsWith(TIMEOUT_REFIX);
    }
}
